package cn.tedu.charging.device.service.impl;

import cn.tedu.charging.common.pojo.param.GunStatusUpdateParam;
import cn.tedu.charging.common.pojo.vo.StationInfoVO;
import cn.tedu.charging.device.dao.repository.DeviceRepository;
import cn.tedu.charging.device.dao.repository.StationCacheRepository;
import cn.tedu.charging.device.pojo.po.StationPO;
import cn.tedu.charging.device.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private DeviceRepository deviceRespository;
    @Autowired
    @Qualifier("CacheGEOANDShareHash")
    private StationCacheRepository stationCacheRepository;
    @Override
    public StationInfoVO getStationInfo(Integer gunId) {
        log.debug("获取场站信息业务，参数：{}", gunId);
        // 对多表联查 进行封装 通过id
        StationInfoVO stationInfoVO = deviceRespository.getStationInfo(gunId);
        /*StationInfoVO stationInfoVO=new StationInfoVO();
        stationInfoVO.setGunStatus(1);*/
        return stationInfoVO;
    }

    @Override
    public Boolean updateGunStatus(GunStatusUpdateParam gunStatusUpdateParam) {
        log.debug("更新枪口状态业务，参数：{}", gunStatusUpdateParam);
        Boolean success = deviceRespository.updateGunStatus(gunStatusUpdateParam);
        log.debug("更新枪口状态业务参数:{},结果:{}",gunStatusUpdateParam, success);
        return success;
    }

    @Override
    public List<StationInfoVO> searchNearStations(Double longitude, Double latitude, Double distance) {
        log.debug("根据经纬度{},{},和距离{}进行搜索",longitude,latitude,distance);
        List<StationPO> stationPOList = stationCacheRepository.search(longitude, latitude, distance);
        log.debug("获取附近充电站信息:{}",stationPOList);
        List<StationInfoVO> stationInfoVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(stationPOList)){

            for (StationPO stationPO : stationPOList) {
                StationInfoVO stationInfoVO = new StationInfoVO();
                BeanUtils.copyProperties(stationPO,stationInfoVO);
                stationInfoVOS.add(stationInfoVO);
            }

        }
        return stationInfoVOS;
        //return Collections.emptyList();

    }


}
